package com.example.base.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.base.ILifecycler;
import com.example.base.R;
import com.example.base.R2;
import com.example.base.utils.BaseFindView;
import com.example.base.utils.ReflectUtils;
import com.example.base.utils.ThemeUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @author Administrator
 */
public abstract class BaseActivity2<P extends IBasePresenter2> extends RxAppCompatActivity
        implements ILifecycler<P> {

    protected P mPresenter;

    @BindView(R2.id.base_head_title_tv)
    protected TextView title;

    /**
     * 头布局背景
     */
    @BindView(R2.id.ny_safeproduction_common_head_iv)
    ImageView headBackgroudIv;

    @BindView(R2.id.ny_safrproduction_common_app_bar)
    AppBarLayout mAppBarLayout;

    private Unbinder unbinder;

    private View view;

    protected BaseFindView baseFindView;

    protected Bundle mSavedInstanceState;

    protected StatusViewHelper2 mStatusViewHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSavedInstanceState = savedInstanceState;

        ThemeUtils.setNoActionBar(this);

        mStatusViewHelper = new StatusViewHelper2(this,initLayout());

        this.setContentView(mStatusViewHelper.getContentLayout());

        //butterknife绑定
        unbinder = ButterKnife.bind(this);

        //初始化滑动头部
        baseFindView = new BaseFindView(view, this);
        baseFindView.initHead();

        //设置appbarlayout监听
        mAppBarLayout = findViewById(R.id.ny_safrproduction_common_app_bar);
        //AppBarScroolUtils.setAppBarListener(this,mAppBarLayout);
        //baseFindView.setAppBarListener();
        initPresenter();

        todo();
    }


    public void setContentView(FrameLayout res) {
        view = View.inflate(this, R.layout.ny_safeproducation_base_activity, null);
        super.setContentView(view);
        ViewGroup container = (ViewGroup) findViewById(R.id.ny_safeproduction_inspection_base_body);
        //View.inflate(this, res, (ViewGroup) findViewById(R.id.ny_safeproduction_inspection_base_body));
        container.addView(res);
    }

    /**
     * 初始化presenter
     */
    private void initPresenter() {
        mPresenter = (P) ReflectUtils.getGenericInstance(this.getClass());
        if (mPresenter != null) {
            mPresenter.onstart();
        }
    }

    /**
     * 设置头布局背景颜色
     *
     * @param resId
     */
    protected void setHeadBackgroudIv(int resId) {
        headBackgroudIv.setImageResource(resId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestory();
            mPresenter = null;
        }

        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
    }
}
